package com.bao.grpc.service.impl;

import com.bao.grpc.lib.UserGrpc;
import com.bao.grpc.lib.UserRequest;
import com.bao.grpc.lib.UserResponse;
import com.bao.grpc.service.UserService;
import io.grpc.Channel;
import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends UserGrpc.UserImplBase implements UserService {

    @GrpcClient("grpc_local_server")
    private Channel channel;

    @Override
    public Map getUser(String name) {
        UserGrpc.UserBlockingStub blockingStub = UserGrpc.newBlockingStub(channel);
        UserResponse response = blockingStub.sayHello(UserRequest.newBuilder().setName(name).build());
        Map map = new HashMap();
        map.put("msg", response.getMessage());
        return map;
    }
}